/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.refresh;

import java.util.*;
import bw.util.*;

public class RefresherPool extends Agent implements RefreshableSource
{
    private static Object _object = new Object();
    private static RefresherPool _instance = null;

    private Vector _sources = new Vector();
    private Vector _refreshables = new Vector();
    //private BlockingWorkerPool _pool = null;
    private RefresherHandler _handler = null;

    public static RefresherPool getInstance()
    {
        if ( _instance == null )
        {
            synchronized ( _object )
            {
                if ( _instance == null )
                {
                    _instance = new RefresherPool();
                }
            }
        }

        return _instance;
    }

    private RefresherPool ()
    {
        //we set the refresh interval for 60 seconds
        super( "RefresherPool", 60 * 1000L );
        
        _sources.addElement( this );
    }
    
    public void setRefresherHandler ( RefresherHandler h )
    {
        _handler = h;
    }

    public void addRefreshable ( Refreshable r )
    {
        //add a single refreshable
        _refreshables.addElement( r );
    }

    public Iterator getRefreshables ()
    {
        return _refreshables.iterator();
    }

    public void addSource ( RefreshableSource source )
    {
        //add a refreshable source -- this returns an
        //iterator of refreshables. 
        _sources.addElement( source );
    }

    public void performAction ()
    {
        //iterate over all our sources of refreshables (including me)
        Iterator sources = _sources.iterator();
        while ( sources.hasNext() )
        {
            RefreshableSource source = (RefreshableSource) sources.next();
            
            //for each source refresh all its refreshables if needed
            Iterator refreshables = null;
            try
            {
                refreshables = source.getRefreshables();
            }
            catch ( Exception ex )
            {
                Log.getInstance().write( "Problem getting refreshables!", ex );
                refreshables = ( new Vector() ).iterator();
            }

            while ( refreshables.hasNext() )
            {
                Refreshable r = (Refreshable) refreshables.next();
                if ( r.shouldRefresh() )
                {
                    _handler.handleRefresh( r );
                }
            }
        }
    }

    public void shutdown ()
    {
        super.shutdown(); 
        _handler.shutdown();
    }


}